package nm.util.alarmclock;

import java.io.Serializable;
import java.io.File;
import javax.swing.JFrame;
import java.awt.event.ActionListener;
import javax.swing.JSpinner;
import javax.swing.JButton;
import javax.swing.JPanel;
import javax.swing.JTextField;
import javax.swing.BoxLayout;
import javax.swing.Box;
import java.awt.GridLayout;
import javax.swing.BorderFactory;
import javax.swing.UIManager;
import java.awt.Toolkit;
import java.awt.Dimension;

public class AlarmGUIFrame extends JFrame implements Serializable {
	public static final long serialVersionUID = 1L;
	public static final String TITLE = "Alarm Clock";
	public static final int SPACER = 5;
	
	public AlarmGUIFrame(ActionListener listener, AlarmSettings settings,
			String soundDirectory) {
		super(AlarmGUIFrame.TITLE);
		
		try
		{
			UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
		}
		catch(Exception exception) {}
		
		start = new JButton("Start");
		start.setActionCommand(AlarmClock.CMD_START);
		stop = new JButton("Stop");
		stop.setActionCommand(AlarmClock.CMD_STOP);
		sfopen = new JButton("...");
		sfopen.setActionCommand(AlarmClock.CMD_CHOOSE);
		
		hour = new AlarmGUIHour(settings.getHour());
		minute = new AlarmGUIMinute(settings.getMinute());
		
		sfile = new JTextField();
		sfile.setEditable(false);
		setSound(settings.getSound());
		
		layoutWindow();
		addActionListener(listener);
		
		setResizable(false);
		pack();		
		center();
	}
	
	public void layoutWindow() {
		JPanel mainPanel = new JPanel();
		mainPanel.setLayout(new BoxLayout(mainPanel, BoxLayout.PAGE_AXIS));
		mainPanel.setBorder(BorderFactory.createEmptyBorder(
				AlarmGUIFrame.SPACER, AlarmGUIFrame.SPACER, 
				AlarmGUIFrame.SPACER, AlarmGUIFrame.SPACER));
		
		JPanel timePanel = new JPanel();
		timePanel.setLayout(new GridLayout(1, 3));
		timePanel.add(hour);
		timePanel.add(minute);
		timePanel.add(start);
		timePanel.add(stop);
		
		JPanel filePanel = new JPanel();
		filePanel.setLayout(new BoxLayout(filePanel, BoxLayout.LINE_AXIS));
		filePanel.add(sfile);
		filePanel.add(Box.createHorizontalGlue());
		filePanel.add(sfopen);
		
		mainPanel.add(timePanel);
		mainPanel.add(Box.createVerticalStrut(AlarmGUIFrame.SPACER));
		mainPanel.add(filePanel);
		this.add(mainPanel);
	}
	
	public void addActionListener(ActionListener listener) {
		if(listener != null)
		{
			start.addActionListener(listener);
			stop.addActionListener(listener);
			sfopen.addActionListener(listener);
		}
	}
	
	public void center() {
		Dimension screen = Toolkit.getDefaultToolkit().getScreenSize();
		setLocation((int)(screen.getWidth() / 2) - (getWidth() / 2),
				(int)(screen.getHeight() / 2) - (getHeight() / 2));
	}
	
	public int getHour() {
		return (Integer)hour.getValue();
	}
	public void setHour(int value) {
		hour.setValue(value);
	}
	
	public int getMinute() {
		return (Integer)minute.getValue();
	}
	public void setMinute(int value) {
		minute.setValue(value);
	}
	
	public File getSound() {
		return sound;
	}
	public void setSound(File file) {
		if(file != null)
		{
			sound = file;
			sfile.setText(file.getName());
		}
		else
		{
			sfile.setText("Sound not selected.");
		}
	}
	
	public void setVisible(boolean value) {
		super.setVisible(value);
		if(value)
		{
			stop.requestFocusInWindow();
		}
	}
	
	//instance variables
	private JButton start, stop, sfopen;
	private JSpinner hour, minute;
	private JTextField sfile;
	private File sound;

}//end AlarmGUIFrame
